package com.lovo.pz.window;

import com.lovo.pz.util.FileUtil;
import com.lovo.pz.util.GameMusic;
import com.lovo.pz.util.GameUtil;
import com.lovo.pz.util.UserBean;

import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
 * 游戏窗口
 */
public class MainFrame extends JFrame {
    // 建立图像追踪器
    private MediaTracker tracker = new MediaTracker(this);
    // 卡片布局管理器
    private CardLayout cardLayout = new CardLayout();
    // 欢迎界面面板
    private WelcomePanel welcomePanel;
    // 菜单界面面板
    private MenuPanel menuPanel;
    // 游戏界面面板
    private GamePanel gamePanel;
    // 排行榜界面面板
    private RankPanel rankPanel;
    // 玩家信息对象
    private UserBean userBean;
    // 玩家信息的集合
    private List<UserBean> users;
    public MainFrame(){

        // 加载保存的玩家集合
        users = FileUtil.read();
        // 图片的加载
        GameUtil.initImage(this,tracker);
        // 设置当前窗口的布局
        this.setLayout(cardLayout);
        welcomePanel = new WelcomePanel(this);
        menuPanel = new MenuPanel(this);
        gamePanel = new GamePanel(this);
        rankPanel = new RankPanel(this);
        // 将子面板添加到当前容器中
        this.add(welcomePanel,"welcome");
        this.add(gamePanel,"game");
        this.add(menuPanel,"menu");
        this.add(rankPanel,"rank");



        // 设置窗口标题文字
        this.setTitle("植物大战僵尸");
        // 将窗口进行重绘
        this.pack();
        // 获取窗口标题栏和边框大小
        Insets insets = this.getInsets();
        this.setSize(900 + insets.left + insets.right
                ,600 + insets.top + insets.bottom);
        // 设置窗口默认的关闭策略
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // 窗口居中显示
        this.setLocationRelativeTo(null);
        // 显示窗口
        this.setVisible(true);
    }
    // 切换面板
    public void switchPanel(String name){
        cardLayout.show(this.getContentPane(),name);
    }

    public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public static void main(String[] args) {
        new MainFrame();
    }

    public GamePanel getGamePanel() {
        return gamePanel;
    }

    public void setGamePanel(GamePanel gamePanel) {
        this.gamePanel = gamePanel;
    }

    public List<UserBean> getUsers() {
        return users;
    }

    public void setUsers(List<UserBean> users) {
        this.users = users;
    }
}
